package com.mnotice.common.utils;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import lombok.SneakyThrows;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

public class PdfUtil {
    @SneakyThrows
    public static void createPdfNoneCss(String content, String dest) {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        //XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        //fontImp.register(FONT);
        XMLWorkerHelper.getInstance()
                .parseXHtml(
                        writer, document,
                        new ByteArrayInputStream(content.getBytes()),
                        null,
                        StandardCharsets.UTF_8
                );
        document.close();
    }

    @SneakyThrows
    public static void createPdfWithCss(String content, String dest) {
        ITextRenderer render = new ITextRenderer();
        //ITextFontResolver fontResolver = render.getFontResolver();
        //fontResolver.addFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        render.setDocumentFromString(content);
        render.layout();
        render.createPDF(new FileOutputStream(dest));
    }
}
